<template>
	<view class="cq-container">
		<customImage :imgSrc="'/clb/clb/108/top.png'" width="750rpx"></customImage>
		<view class="form-container">
			<uni-forms class="forms-item" ref="accountForm" :model="formData">
				<uni-forms-item>
					<inputPhone v-model="formData.mobile" ps="32rpx" pc="#0D0D0D" height="80rpx" lh="80rpx" ta="left"></inputPhone>
				</uni-forms-item>
				<view class="input-between">
					<uni-forms-item>
						<input
							style="width: 262rpx; margin-left: 50rpx"
							maxlength="6"
							type="tel"
							v-model="formData.smsCode"
							placeholder="请输入验证码"
							class="uni-input-input"
							placeholder-style="color: #0D0D0D;font-size:32rpx"
						/>
					</uni-forms-item>
					<uni-forms-item class="btn-bg">
						<view class="code" @click="sendCode" :style="{ cursor: isSend ? 'not-allowed' : '' }">
							{{ isSend ? `重新发送 ${isNum}` : '获取验证码' }}
						</view>
					</uni-forms-item>
				</view>

				<view class="login-btn" style="text-align: center">
					<customImage :imgSrc="'/clb/clb/108/orderBtn.png'" width="616rpx" @click="submitform"></customImage>
				</view>

				<view style="margin-top: 20rpx; margin-bottom: 0rpx; padding-bottom: 0rpx; display: flex; align-items: center">
					<checkbox-group @change="changeAll">
						<label>
							<checkbox value="cb" :checked="allChecked" style="transform: scale(0.7)" />
							<text style="color: #333333; font-weight: 400; font-size: 24rpx">已阅读并同意</text>
						</label>
					</checkbox-group>
					<text style="color: #f65814; font-weight: 400; font-size: 24rpx" @click="privatePro(2)">《产品详情》</text>
					<text style="color: #333333; font-weight: 400; font-size: 24rpx">和</text>
					<text style="color: #f65814; font-weight: 400; font-size: 24rpx" @click="privatePro(1)">《个人信息保护政策》</text>
				</view>
			</uni-forms>
		</view>

		<customImage :imgSrc="'/clb/clb/108/bottom.png'"></customImage>
		<view class="arrive-btn" :style="{ display: showHeight > 700 ? '' : 'none' }">
			<customImage :imgSrc="'/clb/clb/108/orderBtn.png'" width="616rpx" @click="toTop"></customImage>
		</view>

		<!-- 协议 -->
		<uni-popup ref="popupPro" type="bottom" border-radius="10px 10px 0 0">
			<per-info :agreementType="agreementType" @ok="ok1"></per-info>
		</uni-popup>

		<uni-popup ref="resultPop" type="bottom" border-radius="10px 10px 0 0">
			<view class="sure-image">
				<customImage :imgSrc="'/clb/clb/108/result.png'" width="580rpx"></customImage>
			</view>
			<view class="close-sure-btn">
				<customImage :imgSrc="'/clb/clb/108/sureOk.png'" width="380rpx" @click="closeSureBtn"></customImage>
			</view>
		</uni-popup>
	</view>
</template>

<script setup>
import { ref, reactive, onMounted, nextTick, watchEffect, watch } from 'vue';
import { onLoad, onPageScroll, onReady } from '@dcloudio/uni-app';
import initApi from '@/apis/init.js';
import perInfoVue from '../../components/per-info/per-info.vue';
const accountForm = ref();
// 表单数据
const showHeight = ref(0);
const isSend = ref(false);
const allChecked = ref(false);
const popupPro = ref();
const resultPop = ref();
const isNum = ref(0);
const isTimer = ref();
const agree = ref('同意');
const agreementType = ref(1);
const formData = reactive({
	mobile: '',
	smsCode: null,
	orderNo: null,
	channelCode: '',
	productCode: ''
});

const changeAll = (val) => {
	if (val.detail.value.length > 0) {
		allChecked.value = true;
	} else {
		allChecked.value = false;
	}
};

watch(
	() => formData.mobile,
	(newVal, oldValue) => {
		const isOkMobile = /^1[3-9][0-9]\d{8}$/.test(newVal);
		if (isOkMobile) {
			uni.hideKeyboard();
			sendCode();
		}
	}
);
const sendCode = async () => {
	if (!formData.mobile) return uni.utils.toast('请输入手机号！');
	if (isSend.value) return;
	try {
		const arr = {
			mobile: formData.mobile,
			channelCode: formData.channelCode,
			productCode: formData.productCode
		};
		const isOkMobile = /^1[3-9][0-9]\d{8}$/.test(formData.mobile);
		if (!isOkMobile) return uni.utils.toast('请输入正确的手机号');
		const res = await initApi.initInventoryPackageOrder(arr);
		if (res.code != 20000) {
			window.location.href = `https://kshk.91bak.com/web_fcs/?mobile=${formData.mobile}&id=202410241001029140`;
			return uni.utils.toast(res.message);
		}
		formData.orderNo = res.data.orderNo;
		isNum.value = 60;
		isSend.value = true;
		isTimer.value = setInterval(() => {
			if (isNum.value <= 0) {
				clearInterval(isTimer.value);
				isNum.value = 0;
				isSend.value = false;
			}
			isNum.value--;
		}, 1000);
		return uni.utils.toast('验证码发送成功');
	} catch (e) {
		//TODO handle the exception
		isSend.value = false;
		isNum.value = 0;
		clearInterval(isTimer.value);
		window.location.href = `https://kshk.91bak.com/web_fcs/?mobile=${formData.mobile}&id=202410241001029140`;
		uni.utils.toast('请输入正确手机号');
	}
};

const closeSureBtn = () => {
	resultPop.value.close();
};
const successOk = async () => {
	try {
		if (!allChecked.value) return uni.utils.toast('请勾选《个人信息收集》');
		if (!formData.mobile) return uni.utils.toast('请输入手机号');
		if (!formData.smsCode) return uni.utils.toast('请输入验证码');
		const arr = {
			orderNo: formData.orderNo,
			mobile: formData.mobile,
			smsCode: formData.smsCode,
			channelCode: formData.channelCode,
			productCode: formData.productCode
		};
		const res = await initApi.processInventoryPackageOrder(arr);
		if (res.code != 20000) {
			window.location.href = `https://kshk.91bak.com/web_fcs/?mobile=${formData.mobile}&id=202410241001029140`;
			return uni.utils.toast(res.message);
		}
		clearInterval(isTimer.value);
		isNum.value = 0;
		isSend.value = false;
		nextTick(() => {
			resultPop.value.open('center');
		});
	} catch (e) {
		//TODO handle the exception
		console.log(e);
		clearInterval(isTimer.value);
		isNum.value = 0;
		isSend.value = false;
		window.location.href = `https://kshk.91bak.com/web_fcs/?mobile=${formData.mobile}&id=202410241001029140`;
		return uni.utils.toast('订单订购失败');
	}
};
/* no  直接下单 */
const submitform = async () => {
	successOk();
};

onLoad((query) => {
	if (!query.a || !query.p) {
		uni.reLaunch({
			url: '/pages/clb/404'
		});
	}

	formData.channelCode = query.a;
	formData.productCode = query.p;
});
const toTop = () => {
	uni.pageScrollTo({
		scrollTop: 0,
		duration: 300
	});
};
onPageScroll((e) => {
	showHeight.value = e.scrollTop;
});

const ok1 = () => {
	popupPro.value.close();
	allChecked.value = true;
};

const privatePro = (type) => {
	agreementType.value = type;
	popupPro.value.open('center');
};
</script>

<style lang="scss" scoped>
.arrive-btn {
	position: fixed;
	bottom: 20rpx;
	width: 750rpx;
	text-align: center;
}
.cq-container {
	width: 750rpx;
	margin: 0 auto;
	background-color: #93e0fe;
	box-sizing: border-box;
	overflow-x: hidden;
	/* 表单 */
	.form-container {
		height: 540rpx;
		width: 620rpx;
		margin: 0 auto;
	}
	.login-btn {
		animation: scaleUp 2.5s infinite; /* 动画名称、持续时间、是否无限循环 */
	}
	@keyframes scaleUp {
		0%,
		50% {
			transform: scale(1); /* 开始时的缩放比例 */
		}
		25%,
		75% {
			transform: scale(1.05); /* 缩放比例最大值 */
		}
		100% {
			transform: scale(1); /* 结束时的缩放比例 */
		}
	}
	.uni-forms-item {
		height: 80rpx;
		margin-bottom: 20 !important;
		border-bottom: 2rpx solid #eee;
		background-color: #ffffff;
		border-radius: 100rpx;
		border: 2rpx solid #3390ff;
		box-sizing: border-box;
		.uni-input-input {
			width: 616rpx;
			margin-left: 50rpx;
		}
		::v-deep .uni-forms-item__error {
			width: 100%;
			padding-top: 10rpx;
			color: #fff;
			text-align: center;
			font-size: 28rpx;
			transition: none;
		}
	}
	.input-between {
		display: flex;
		justify-content: space-between;
		.btn-bg {
			background-color: #3390ff;
		}
		.code {
			width: 280rpx;
			text-align: center;
			color: #ffffff;
			line-height: 80rpx;
		}
	}
}
.cancel-btn {
	display: flex;
	margin-top: 33rpx;
	justify-content: space-between;
}
.close-sure-btn {
	margin-top: 54rpx;
	display: flex;
	justify-content: center;
}
::v-deep {
	image {
		vertical-align: bottom; // 解决image背景图片下方有10rpx 的白边
	}
}
:deep(.uni-checkbox-input) {
	border-radius: 80rpx !important;
}
</style>
